package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by xndong on 10/2/14.
 */
public class Library {
    static ArrayList<Item> bookList = new ArrayList<Item>();


    public Library(){
        bookList.add(new Item("thoughtworks","tw",2014, false));
        bookList.add(new Item("Clean Code","C.C",2014, false));
        bookList.add(new Item("Head First Java","H.F",2000, false));
        bookList.add(new Item("Head First HTML","H.F",2000, false));
        bookList.add(new Item("Head First CSS","H.F",2000, false));
        }

    public Library(Item item){
        this.bookList.add(item);
    }

    public Library(ArrayList<Item> items){
        this.bookList = items;
    }

    public static void Return(String input, Customer currentUser) {
        Item book = GetItem(input);
        book.Return(currentUser);
        UpdateLibrary(book);
    }

    public static void CheckOut(String input, Customer currentUser) {
        Item book = GetItem(input);
        book.CheckOut(currentUser);
        UpdateLibrary(book);
    }

    private static void UpdateLibrary(Item book) {
        bookList.remove(book);
        bookList.add(book);
    }

    private static Item GetItem(String input){
        if(input.isEmpty()) return null;
        int index = Integer.parseInt(input) - 1;
        Iterator<Item> it = bookList.iterator();
        while (it.hasNext()) {
            if( it.next().Id == index) return it.next();
        }
        return null;
    }
}
